package com.raining.inningz.ui.play;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.raining.inningz.R;
import com.raining.inningz.baseclass.BaseFragment;
import com.raining.inningz.constants.IDBConstants;
import com.raining.inningz.constants.IFragmentNameConstants;
import com.raining.inningz.constants.IParamConstants;
import com.raining.inningz.custom.validator.Form;
import com.raining.inningz.custom.validator.Validate;
import com.raining.inningz.custom.validator.validator.NotEmptyValidator;
import com.raining.inningz.database.dao.Matches;
import com.raining.inningz.database.dao.MatchesDao.Properties;
import com.raining.inningz.utils.Utils;

public class PlayTossFragment extends BaseFragment implements
		OnClickListener {

	private LayoutInflater mInflater;
	private View view;

	
	private RelativeLayout relativeLayoutVisitingTeam;
	private RelativeLayout relativeLayoutHostingTeam;
	
	private RelativeLayout relativeLayoutBat;
	private RelativeLayout relativeLayoutBowl;
	
	
	
	ImageView imgVisitingTeam;
	ImageView imgHostingTeam;
	ImageView imgBat;
	ImageView imgBowl;
	
	
	
	private TextView txtVisitingTeam;
	
	private TextView txtHostingTeam;
	
	private TextView txtHostingTeamPlayer;
	private TextView txtOfficials;
	
	private LinearLayout lstContainerMain;
	
	private PlayMatchInfoFragment matchInfoFragment;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			
			
			matchID=getArguments().getString(IParamConstants.MATCH_ID);
			
			isEdit=getArguments().getBoolean(IParamConstants.IS_EDIT);
			}
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mInflater = inflater;

		view = inflater.inflate(R.layout.fragment_play_toss,
				container, false);
		context = mInflater.getContext();

		initialiseUI();

		setListner();
		
		setData();

		return view;
	}
	

	private void setData() {
		
		relativeLayoutVisitingTeam.setTag(false);
		relativeLayoutHostingTeam.setTag(false);
		
		relativeLayoutBat.setTag(false);
		relativeLayoutBowl.setTag(false);
		
		imgVisitingTeam.setTag(false);
		imgHostingTeam.setTag(false);
		imgBat.setTag(false);
		imgBowl.setTag(false);
		
		
		
	}

	private void initialiseUI() {

		setHeaderIntialize(view);
		setLeftReturn();
		setLeftTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "InningZ");
		setRightTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "Save");
		setHeaderTitle(resources.getString(R.string.about), Color.BLACK);

	
		relativeLayoutVisitingTeam = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutVisitingTeam);
		relativeLayoutHostingTeam = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutHostingTeam);
		
		relativeLayoutBat = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutBat);
		relativeLayoutBowl = (RelativeLayout) view
				.findViewById(R.id.relativeLayoutBowl);
		
		
		
	
		txtVisitingTeam=(TextView)view.findViewById(R.id.txtVisitingTeam);
	
		txtHostingTeam=(TextView)view.findViewById(R.id.txtHostingTeam);
		
		
		
		imgVisitingTeam=(ImageView)view.findViewById(R.id.imgVisitingTeam);
		imgHostingTeam=(ImageView)view.findViewById(R.id.imgHostingTeam);
		imgBat=(ImageView)view.findViewById(R.id.imgBat);
		imgBowl=(ImageView)view.findViewById(R.id.imgBowl);
		
		lstContainerMain=(LinearLayout)view.findViewById(R.id.lstContainerMain);

	}

	private void setListner() {

		getBtnLeft().setOnClickListener(this);
		getBtnRight().setOnClickListener(this);
		
		relativeLayoutVisitingTeam.setOnClickListener(this);
		relativeLayoutHostingTeam.setOnClickListener(this);
		relativeLayoutBat.setOnClickListener(this);
		relativeLayoutBowl.setOnClickListener(this);
		
		

	}

	@Override
	public void onClick(View v) {

		if (v == getBtnLeft()) {
			fragmentManager.popBackStack();
		} else if (v == getBtnRight()) {
			
			Utils.exportDatabse(IDBConstants.DB_NAME, context);

		} else if (v == relativeLayoutVisitingTeam) {
			
			
			
			setUIModulation(relativeLayoutVisitingTeam);

		} else if (v == relativeLayoutHostingTeam) {
			
			
			
			setUIModulation(relativeLayoutHostingTeam);

		}
		else if (v == relativeLayoutBat) {
			
			
			
			setUIModulation(relativeLayoutBat);

		}
		else if (v == relativeLayoutBowl) {
		
			
			setUIModulation(relativeLayoutBowl);
		}

	}


	private void setUIModulation(View view) {
		
		
		if((view== relativeLayoutVisitingTeam) && (view.getTag().equals(true))){
			relativeLayoutVisitingTeam.setTag(false);
			
			relativeLayoutBat.setTag(false);
			relativeLayoutBowl.setTag(false);
		}
		else if((view== relativeLayoutHostingTeam) && (view.getTag().equals(true))){
			relativeLayoutHostingTeam.setTag(false);
			
			relativeLayoutBat.setTag(false);
			relativeLayoutBowl.setTag(false);
		}
		
		
	    else if(view==relativeLayoutVisitingTeam){
			relativeLayoutVisitingTeam.setTag(true);
			relativeLayoutHostingTeam.setTag(false);
			
			relativeLayoutBat.setTag(false);
			relativeLayoutBowl.setTag(false);
		}
		else if(view== relativeLayoutHostingTeam){
			relativeLayoutVisitingTeam.setTag(false);
			relativeLayoutHostingTeam.setTag(true);
			
			relativeLayoutBat.setTag(false);
			relativeLayoutBowl.setTag(false);
		}
		else if(view== relativeLayoutBat){
			relativeLayoutBat.setTag(true);
			relativeLayoutBowl.setTag(false);
		}
		else if(view==relativeLayoutBowl){
			relativeLayoutBat.setTag(false);
			relativeLayoutBowl.setTag(true);
		}
		
		
		
		
		
		if((relativeLayoutVisitingTeam.getTag().equals(true)) || (relativeLayoutHostingTeam.getTag().equals(true) )){
			lstContainerMain.setVisibility(View.VISIBLE);
		}
		else{
			lstContainerMain.setVisibility(View.INVISIBLE);
		}
		
		
		
		if(relativeLayoutVisitingTeam.getTag().equals(true)){
			imgVisitingTeam.setVisibility(View.VISIBLE);
		}
		else if(relativeLayoutVisitingTeam.getTag().equals(false)){
			imgVisitingTeam.setVisibility(View.INVISIBLE);
		}
		
		
		
		if(relativeLayoutHostingTeam.getTag().equals(true)){
			imgHostingTeam.setVisibility(View.VISIBLE);
		}else if(relativeLayoutHostingTeam.getTag().equals(false)){
			imgHostingTeam.setVisibility(View.INVISIBLE);
		}
		
		
		
		if(relativeLayoutBat.getTag().equals(true)){
			imgBat.setVisibility(View.VISIBLE);
		}
		else if(relativeLayoutBat.getTag().equals(false)){
			imgBat.setVisibility(View.INVISIBLE);
		}
		
		
		
		if(relativeLayoutBowl.getTag().equals(true)){
			imgBowl.setVisibility(View.VISIBLE);
		}else if(relativeLayoutBowl.getTag().equals(false)){
			imgBowl.setVisibility(View.INVISIBLE);
		}
		
		
	}
}
